package cn.iocoder.yudao.module.shop.info.vo;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.audit.enums.AuditStatusEnum;
import cn.iocoder.yudao.framework.business.basic.enums.EnumCodable;
import cn.iocoder.yudao.framework.business.basic.pojo.bo.BusinessHours;
import cn.iocoder.yudao.framework.business.basic.pojo.bo.TemplateParameter;
import cn.iocoder.yudao.framework.business.eligibility.pojo.vo.EligibilityApplyVO;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import cn.iocoder.yudao.framework.region.vo.setter.CountyAutoSetter;
import cn.iocoder.yudao.framework.validation.InEnumCode;
import cn.iocoder.yudao.framework.validation.InEnumValue;
import cn.iocoder.yudao.module.api.shop.category.eums.ShopCategoryCodeEnum;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.constant.DictTypeConstants.RECOMMEND_TYPE;
import static cn.iocoder.yudao.framework.constant.DictTypeConstants.SCORE_LEVEL;
import static cn.iocoder.yudao.module.api.shop.DictTypeConstants.SHOP_CATEGORY_CODE;
import static cn.iocoder.yudao.module.api.shop.DictTypeConstants.SHOP_TYPE;

@Schema(description = "店铺信息 Response VO")
@Data
public class ShopInfoVO extends EligibilityApplyVO implements BusinessHours, CountyAutoSetter {

    @Schema(description = "主营类目编号", example = "1024")
    private Long categoryId;

    @Schema(description = "主营类目编码：mall-产品类、food-美食类、hotel-住宿类、entertainment-娱乐类、housekeeping-家政类", example = "mall")
    @DictFormat(SHOP_CATEGORY_CODE)
    @InEnumCode(ShopCategoryCodeEnum.class)
    private String categoryCode;

    @Schema(description = "主营类目名称", example = "酒店住宿")
    private String categoryName;

    public void assignCategory(EnumCodable codable) {
        if (codable != null) {
            setCategoryCode(codable.getCode());
            setCategoryName(codable.getName());
        } else {
            setCategoryCode(ShopCategoryCodeEnum.UNDEFINED.getCode());
            setCategoryName(ShopCategoryCodeEnum.UNDEFINED.getName());
        }
    }

    @Schema(description = "商家类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @DictFormat(SHOP_TYPE)
    private Integer type;

    @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "羡民")
    private String name;

    @Schema(description = "徽标", requiredMode = Schema.RequiredMode.REQUIRED)
    private String logo;

    @Schema(description = "口号", example = "大家好，才是真的好")
    private String slogan;

    @Schema(description = "门头照")
    private String banner;

    @Schema(description = "是否平台自营", example = "false")
    private Boolean selfOperated;

    @Schema(description = "区县编码", example = "520102")
    private String countyCode;

    @Schema(description = "区县名称", example = "南明区")
    private String countyName;

    @Schema(description = "城市编码", example = "520100")
    private String cityCode;

    @Schema(description = "城市名称", example = "贵阳市")
    private String cityName;

    @Schema(description = "省份编码", example = "5200000")
    private String provinceCode;

    @Schema(description = "省份名称", example = "贵州省")
    private String provinceName;

    @Schema(description = "省市区县", deprecated = true, example = "贵州省贵阳市南明区")
    @Deprecated
    private String provinceCityCounty = "";

    @SuppressWarnings("unused")
    public String getProvinceCityCity() {
        if (StrUtil.isBlank(provinceCityCounty)) {
            return getProvinceName() + getCityName() + getCountyName();
        }
        return provinceCityCounty;
    }

    @Schema(description = "详细地址", example = "花果园金融街5号驰通集团公司")
    private String detailAddress;

    @Schema(description = "完整地址", example = "贵州省贵阳市南明区花果园金融街5号驰通集团公司")
    private String fullAddress;

    @Schema(description = "推荐类型：0-无、1-精选、2-热门、99-置顶", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    @DictFormat(RECOMMEND_TYPE)
    private Integer recommend;

    @Schema(description = "形象图集", example = "")
    private List<String> picUrls;

    @SuppressWarnings("unused")
    public List<String> getPicUrls() {
        if (picUrls == null) {
            return Collections.emptyList();
        }
        return picUrls;
    }

    @Schema(description = "标签集", example = "")
    private List<String> labels;

    @SuppressWarnings("unused")
    public List<String> getLabels() {
        if (labels == null) {
            return Collections.emptyList();
        }
        return labels;
    }


    @Schema(description = "简介")
    private String introduction;

    @Schema(description = "公告")
    private String notice;

    @Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer status;

    @Schema(description = "累计销量", example = "1024")
    private Integer salesCount;

    @Schema(description = "累计浏览量", example = "1024")
    private Integer browseCount;

    @Schema(description = "评分", example = "5.0")
    private float score;
    @Schema(description = "评分星级：0-5", example = "0")
    @DictFormat(SCORE_LEVEL)
    private Integer scoreLevel;

    @Schema(description = "服务保障，JSON数组")
    private List<TemplateParameter> serviceBenefits;
    @Schema(description = "服务保障")
    private List<String> serviceBenefitsFormat;

    @Schema(description = "营业开始时间", example = "08:00")
    private LocalTime openingTime;

    @Schema(description = "营业结束时间", example = "22:00")
    private LocalTime closingTime;

    @SuppressWarnings("unused")
    @Schema(description = "营业时间")
    public String getBusinessHours() {
        return formatBusinessHours(getOpeningTime(), getClosingTime());
    }

    @Schema(description = "套餐编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long enterPackageId;

    @Schema(description = "套餐名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "¥199.00/1年")
    private String enterPackageName;

    @Schema(description = "入驻时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime enterTime;

    @Schema(description = "过期时间")
    private LocalDateTime expireTime;

    @Schema(description = "平台服务费百分比，范围为 0-100 之间的数字", example = "10")
    private Double serviceFeePercent;

    @Schema(description = "顾客可抵扣百分比，范围为 0-100 之间的数字", example = "5")
    private Double discountPercent;

    @Schema(description = "下单范围半径，0 为不限制，单位：米", example = "5000")
    private Integer o2oRadius;

    @Schema(description = "纬度", example = "26.567322")
    private Double latitude;

    @Schema(description = "经度", example = "106.690801")
    private Double longitude;

    @Schema(description = "是否新店", example = "false")
    @JsonProperty("new")
    private boolean isNew;

    @SuppressWarnings("unused")
    @JsonProperty("new")
    public boolean isNew() {
        return enterTime != null && DateUtils.differentDays(enterTime, LocalDateTime.now()) <= 60;
    }

    @Schema(description = "审核状态：0-待审核、1-已通过、2-被驳回", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @InEnumValue(AuditStatusEnum.class)
    private Integer auditStatus;

    @Schema(description = "审核说明", requiredMode = Schema.RequiredMode.REQUIRED, example = "营业执照不清晰")
    private String auditNote;

    @Schema(description = "审核时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime auditTime;

    @Schema(description = "开关", example = "0")
    private Integer enabled;

}
